package com.ruoyi.student.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.student.domain.SysStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.student.domain.SysClass;
import com.ruoyi.student.service.ISysClassService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 班级Controller
 * 
 * @author Xsw
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/class")
public class SysClassController extends BaseController
{
    @Autowired
    private ISysClassService sysClassService;

    /**
     * 查询传入校区下所有的班级列表
     */
    @GetMapping("/list/getClassByCampus/{campus}")
    public TableDataInfo getClassByCampus(@PathVariable String campus)
    {
        startPage();
        List<SysClass> list = sysClassService.getClassByCampus(campus);
        return getDataTable(list);
    }

    /**
     * 查询班级下所有学生列表
     */
    @GetMapping("/list/classAndStudents")
    public TableDataInfo classAndStudents()
    {
        startPage();
        List<SysClass> list = sysClassService.classAndStudents();
        return getDataTable(list);
    }

    /**
     * 查询班级列表
     */
    @RequiresPermissions("student:class:list")
    @GetMapping("/list")
    public TableDataInfo list(SysClass sysClass)
    {
        startPage();
        List<SysClass> list = sysClassService.selectSysClassList(sysClass);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @RequiresPermissions("student:class:export")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysClass sysClass)
    {
        List<SysClass> list = sysClassService.selectSysClassList(sysClass);
        ExcelUtil<SysClass> util = new ExcelUtil<SysClass>(SysClass.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 导出班级模版
     * @param response
     */
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<SysClass> list = new ArrayList<>();
        ExcelUtil<SysClass> util = new ExcelUtil<SysClass>(SysClass.class);
        util.exportExcel(response, list, "班级列表数据");
    }

    /**
     * 导入班级列表
     */
    @PostMapping("/import")
    public R<Void> importStudent(MultipartFile file) {
        try {
            ExcelUtil<SysClass> util = new ExcelUtil<SysClass>(SysClass.class);
            List<SysClass> sysClass = util.importExcel(file.getInputStream());
            sysClassService.importClass(sysClass);
        } catch (IOException e) {
            e.printStackTrace();
            return R.fail("上传失败");
        }
        return R.ok();
    }

    /**
     * 获取班级详细信息
     */
    @RequiresPermissions("student:class:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysClassService.selectSysClassById(id));
    }

    /**
     * 新增班级
     */
    @RequiresPermissions("student:class:add")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysClass sysClass)
    {
        return toAjax(sysClassService.insertSysClass(sysClass));
    }

    /**
     * 修改班级
     */
    @RequiresPermissions("student:class:edit")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysClass sysClass)
    {
        return toAjax(sysClassService.updateSysClass(sysClass));
    }

    /**
     * 删除班级
     */
    @RequiresPermissions("student:class:remove")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysClassService.deleteSysClassByIds(ids));
    }
}
